package com.loongtech.bi.manager.realTime;

import com.loongtech.bi.entity.realTime.EntityAddCashUserByDay;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.Time;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 实时数据-日付费帐号数manager
 * 
 * @author marin
 * @date 2015-09-15
 */
@Service
public class AddCashUserByDayManager extends ManagerQueryCacheBase<EntityAddCashUserByDay> {
	private static final long serialVersionUID = 1L;

    // 得到符合国家、区服条件的查看日付费帐号数总和
	public String getAllPayUserByConditionsByTag(Date lookDate, String queryPlatform, String queryChannel, String queryServer, String queryCountry) {
        List<Object[]> oo = findByQuery("select day,sum(addcash_user) from EntityAddCashUserByDayByTag where day>=? and day<? and platformid=? and channelid=? and serverid=? and tagvalue = ? and addcash_user is not null", new Date(Time.getBeginningOfDay(lookDate.getTime())), new Date(Time.getBeginningOfDay(lookDate.getTime() + Time.DAY)),queryPlatform, queryChannel, queryServer, queryCountry);
		return getResult(oo);
	}

    // 得到符合平台、渠道、区服条件的查看日付费帐号数总和
    public String getAllPayUserByConditions(Date lookDate, String queryPlatform, String queryChannel, String queryServer) {
        List<Object[]> oo = findByQuery("select day,sum(addcash_user) from EntityAddCashUserByDay where day>=:dayStart and day<:dayEnd and platformid=:platformid and channelid=:channelid and serverid=:serverid and addcash_user is not null", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "dayEnd", new Date(Time.getBeginningOfDay(lookDate.getTime() + Time.DAY)), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
        return getResult(oo);
    }

	// 根据查询结果，拼装最终结果
	private String getResult(List<Object[]> oo) {
		String result = null;
		if (oo != null && oo.size() > 0 && oo.get(0)[0] != null) {
			int addcash_user = 0;
			for(Object[] o : oo) {
				addcash_user += Integer.parseInt(o[1].toString());
			}
			result = addcash_user + "";
		}
		
		return result;
	}
	
	public List<Object[]> getPaltformPayUserRealTimeDatas(Date lookDate) {
		return findByQuery("select platformid,addcash_user from EntityAddCashUserByDay where day=:dayStart and channelid = :channelid and serverid = :serverid", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "channelid", "0", "serverid", "0");
	}
	
	public List<Object[]> getChannelPayUserRealTimeDatas(Date lookDate) {
		return findByQuery("select channelid,addcash_user from EntityAddCashUserByDay where day=:dayStart and platformid=:platformid and channelid != :channelid and serverid = :serverid", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "platformid", "0", "channelid", "0", "serverid", "0");
	}

    public List<Object[]> getServerPayUserRealTimeDatas(Date lookDate) {
        return findByQuery("select serverid,addcash_user from EntityAddCashUserByDay where day=:dayStart and platformid=:platformid and channelid = :channelid and serverid != :serverid", "dayStart", new Date(Time.getBeginningOfDay(lookDate.getTime())), "platformid", "0", "channelid", "0", "serverid", "0");
    }
}
